<template>
    <!--

    推荐 weight_desc | 热门

    标题
    描述(简介 xxx/xxx/xxx 时间)

    内容

    落款(点赞/浏览...)
    -->

    <div class="article-page">
      <nav class="my-nav">
        <a
        href="javascript:;"
        :class="{active : sorter == 'weight_desc'}"
        @click="changeSorter('weight_desc')"
        >推荐</a>

        <a
        href="javascript:;"
        :class="{active : sorter == null}"
        @click="changeSorter(null)"
        >热门</a>
      </nav>

      <!--  list -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="------我是有底线的-----"
        @load="onLoad"
      >

      <!--  每一项  ActicleItem  抽出去 -->
      <ArticleItem v-for=" item in list" :key="item.id" :item="item"></ArticleItem>
      </van-list>
    </div>
</template>

<script>
// 局部引入

import { getArticle } from '@/api/article'

export default {

  data () {
    return {
      // 列表页 (分页)
      list: [],
      // 当前页码7 , 每一页多少条10
      current: 1,
      sorter: 'weight_desc', // 推荐
      pageSize: 10,
      // 异步加载数据 , 是否在加载中
      loading: false,
      // 是否加载了全部数据
      finished: false
    }
  },
  async created () {
    // axios
    // const res = await getArticle({
    //   current: this.current,
    //   sorter: this.sorter
    // })
    // this.list = res.data.rows
  },
  methods: {
    async onLoad () {
      // this.loading = true
      // 触底了之后  拿新数据
      const res = await getArticle({
        current: this.current,
        sorter: this.sorter,
        pageSize: this.pageSize
      })
      // [{},{},{}]             {},{},{}
      // 累加数据
      this.list.push(...res.data.rows)
      // 不知道在哪true的
      this.loading = false
      // current
      this.current++ // 当前页+1

      //   finished
      if (this.current > res.data.pageTotal) {
        // 没有更多的数据了
        this.finished = true
      }
    },
    changeSorter (value) {
      // 修改类型规则
      this.sorter = value

      // 数据要被重置
      this.current = 1
      this.list = []
      this.finished = false // 又有数据可以加载了

      this.loading = true
      // 按新条件重新加载数据
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
